package work.vcloud.baseinfo.controller;

import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import work.vcloud.frame.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;

import work.vcloud.baseinfo.vo.ExceptionVo;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Scope("prototype")
public class DataController extends BaseController {

	@Autowired
	OpenDao openDao;
	

    /**
     * 验证token
     */
	public void validateApp(Map<String, Object> map) throws Exception {
		map.put("sqlMapId", "app_api.AccessTokenDetail");
		Object tokenVo = openDao.queryForObject(map);
		if(tokenVo != null) {
			Map tokenMap = (Map) tokenVo;
			map.put("client_id", tokenMap.get("client_id"));
			map.put("sqlMapId", "api_deploy.selectPower");
			Integer result = (Integer) openDao.queryForObject(map);
			if (result <= 0) {
				   throw new ExceptionVo("4005","no authority");
			}
		}else{
            throw new ExceptionVo("4006","invalid access_token");
		}
	}
	
    /**
     * 验证token
     */
	public void validate(Map<String, Object> map) throws Exception {
		map.put("sqlMapId", "app_api.AccessTokenDetail");
		Object tokenVo = openDao.queryForObject(map);
		if(tokenVo != null) {
			Map tokenMap = (Map) tokenVo;
			map.put("client_id", tokenMap.get("client_id"));
			map.put("openid", tokenMap.get("openid"));
			try {
				getParamList(map);
			} catch (Exception e) {
                throw new ExceptionVo("4005","no authority");
			}
		}else{
            throw new ExceptionVo("4006","invalid access_token");
		}
	}

	/**
	 * 获取接口参数
	 */
	public void getParamList(Map<String, Object> map) throws Exception {

		map.put("sqlMapId", "app_api.selectByName");

		Map result = (Map) openDao.queryForObject(map);
		if(null == result || null == result.get("aa_pk")) {
			throw new ExceptionVo("4005", "no authority");
		}else{
			result.put("sqlMapId", "app_api.paramsList");
			result.put("client_id", map.get("client_id"));
			List<Map<String, Object>> apiParams = openDao.queryForList(result);
			StringBuffer apiParamsSql = new StringBuffer();
			for (int i = 0; i < apiParams.size(); i++) {
				apiParamsSql.append(","+apiParams.get(i).get("param_column")+" \""+apiParams.get(i).get("param_name")+"\"");
			}
			//去掉第一个逗号
			if(!"".equals(apiParamsSql.toString())) {
				map.put("apiParamsSql", apiParamsSql.substring(1));
			}
		}

	}

    /**
     *  获取接口名称
     *
     */
	public String getApiName(String requestUri) {
        return requestUri.substring(requestUri.lastIndexOf("/")+1, requestUri.length());
    }
	
	/**
     *  获取用户信息接口
     *
     */
    @RequestMapping("oAuth/api/getUserInfo")
    public OutputVo getUserInfo() {
        try {
            Map<String, Object> map = getParameterMap();
            
            //临时处理材料工程综管对接问题
            System.out.println("params1:"+map);
            if(map.get("openid") != null) {
            	String openid = map.get("openid").toString();
            	if(openid.endsWith("?")) {
            		openid = openid.substring(0, openid.length() - 1);
            		map.put("openid", openid);
            		System.out.println("params2:"+map);
            	}
            }
            
            map.put("api_name", getApiName(request.getRequestURI()));
            validate(map);

            map.put("sqlMapId", "app_api.validateUserType");
            Map<String, Object> userMap = (Map)openDao.queryForObject(map);
            
            String userType = "";
            if(userMap == null) {
            	userType = "7";
            	map.put("sqlMapId", "api_student.selectNewById");
            }else {
	            userType = userMap.get("type").toString();
	            
	            if("1".equals(userType)) {
	            	map.put("sqlMapId", "api_teacher.selectById");
	            }else if("2".equals(userType)) {
	            	map.put("sqlMapId", "api_student.selectById");
	            }else if("3".equals(userType)) {
	            	map.put("sqlMapId", "api_parent.selectById");
	            }
            }
            
            Map userInfo = (Map)openDao.queryForObject(map);
            userInfo.put("type", userType);

            return output("0000", "success", userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999", "system exception", "");
        }
    }
    
	/**
	 * 添加用户信息
	 */
	@RequestMapping("oAuth/api/addUserData")
	public OutputVo addUserData() {
		try {
			Map<String, Object> map = getParameterMap();
			
			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);
			
			map.remove("page");
			
			map.put("sqlMapId", "api_user.validateIdcard");
			Integer r1 = (Integer)openDao.queryForObject(map);
			if(r1 > 0) {
				return output("4444", "", "身份证号已存在");
			}
			
			map.put("sqlMapId", "api_user.validatePhone");
			Integer r2 = (Integer)openDao.queryForObject(map);
			if(r2 > 0) {
				return output("4444", "", "手机号已存在");
			}
			
			map.put("fk_user", map.get("id"));
			map.put("sqlMapId", "api_user.insert");
			openDao.insert(map);
			
			map.put("sqlMapId", "api_user.insertUserTeacher");
			openDao.insert(map);
			
			map.put("sqlMapId", "api_user.insertUserRole");
			openDao.insert(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "添加出错", e);
		}
	}

    /**
     * 获取学校信息列表接口
     *
     */
    @RequestMapping("oAuth/api/getSchoolData")
    public OutputVo getSchoolData() {
        try {
            Map<String, Object> map = getParameterMap();

            map.put("api_name", getApiName(request.getRequestURI()));
            validate(map);

            PageVo page = readPage(request);
            if(null == page){
                page = new PageVo();
                page.setNumber(50); //默认为一次请求为50条数据
            }else{
                //一次最多返回500条数据
                if(page.getNumber() > 500){
                    page.setNumber(500);
                }else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
                    page.setNumber(50);
                }
            }

            Object currentPage = map.get("currentPage");
            Object number = map.get("number");
            if(null != currentPage){
                page.setCurrent(Integer.parseInt(currentPage.toString()));
            }
            if(null != number){
                page.setNumber(Integer.parseInt(number.toString()));
            }
            map.put("page", page);

            map.put("sqlMapId", "api_school.select");
            List<Map<String, Object>> dataList = openDao.queryForList(map);
            return output("0000", dataList, page);
        } catch (ExceptionVo e) {
            return output(e.getCode(), e.getMsg(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999", "system exception", "");
        }
    }

	/**
	 * 获取教师信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getTeacherData")
	public OutputVo getTeacherData() {
		try {
			Map<String, Object> map = getParameterMap();

            map.put("api_name", getApiName(request.getRequestURI()));
            validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_teacher.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
            return output(e.getCode(), e.getMsg(), "");
        } catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}

	/**
	 * 获取学生信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getStudentData")
	public OutputVo getStudentData() {
		try {
			Map<String, Object> map = getParameterMap();

            map.put("api_name", getApiName(request.getRequestURI()));
            validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_student.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}

    /**
     * 获取家长信息列表接口
     *
     */
    @RequestMapping("oAuth/api/getParentData")
    public OutputVo getParentData() {
        try {
            Map<String, Object> map = getParameterMap();

            map.put("api_name", getApiName(request.getRequestURI()));
            validate(map);

            PageVo page = readPage(request);
            if(null == page){
                page = new PageVo();
                page.setNumber(50); //默认为一次请求为50条数据
            }else{
                //一次最多返回500条数据
                if(page.getNumber() > 500){
                    page.setNumber(500);
                }else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
                    page.setNumber(50);
                }
            }

            Object currentPage = map.get("currentPage");
            Object number = map.get("number");
            if(null != currentPage){
                page.setCurrent(Integer.parseInt(currentPage.toString()));
            }
            if(null != number){
                page.setNumber(Integer.parseInt(number.toString()));
            }
            map.put("page", page);

            map.put("sqlMapId", "api_parent.select");
            List<Map<String, Object>> dataList = openDao.queryForList(map);
            return output("0000", dataList, page);
        } catch (ExceptionVo e) {
            return output(e.getCode(), e.getMsg(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999", "system exception", "");
        }
    }

	/**
	 * 获取年级信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getGradeData")
	public OutputVo getGradeData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_grade.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}

	/**
	 * 获取班级信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getClassData")
	public OutputVo getClassData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_class.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}

	/**
	 * 获取系统配置列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getPropData")
	public OutputVo getPropData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_prop.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}

	/**
	 * 获取课程列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getCourseData")
	public OutputVo getCourseData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_course.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}


	/**
	 * 获取菜单信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getMenuData")
	public OutputVo getMenuData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);
			map.put("sqlMapId", "api_menu.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 * 获取当前菜单以及其所有子菜单列表信息
	 *
	 */
	@RequestMapping("oAuth/api/getMenuChildsData")
	public OutputVo getMenuChildsData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);
			map.put("sqlMapId", "api_menu.selectAllChild");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	/**
	 * 获取学段信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getButtonData")
	public OutputVo getButtonData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_button.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 * 获取用户角色列表信息接口
	 *
	 */
	@RequestMapping("oAuth/api/getUserRoleData")
	public OutputVo getUserRoleData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_userrole.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 * 添加用户角色
	 *
	 */
	@RequestMapping("oAuth/api/addUserRoleData")
	public OutputVo addUserRoleData() {
		try {
			Map<String, Object> map = getParameterMap();
			map.put("api_name", getApiName(request.getRequestURI()));
			validateApp(map);
			JSONArray list = JSONArray.parseArray(map.get("list").toString());
			if(list.size() > 0) {
				for(int i = 0;i<list.size();i++) {
                    Map<String, Object> vaule = (Map<String, Object>) list.get(i);
                    map.put("fk_role",vaule.get("fk_role"));
                    map.put("fk_user",vaule.get("fk_user"));
                    map.put("create_by",vaule.get("create_by"));
                    
                    map.put("sqlMapId", "api_userrole.selectIfExit");
        			Integer result = (Integer) openDao.queryForObject(map);
        			if(result<=0) {
        				map.put("sqlMapId", "api_userrole.insert");
                        openDao.insert(map);
        			}
                }
			}
			return output("0000");
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 * 修改用户角色
	 *
	 */
	@RequestMapping("oAuth/api/updateUserRoleData")
	public OutputVo updateUserRoleData() {
		try {
			Map<String, Object> map = getParameterMap();
			map.put("api_name", getApiName(request.getRequestURI()));
			validateApp(map);
			
			map.put("fk_role", map.get("fk_role_new"));
			map.put("sqlMapId", "api_userrole.selectIfExit");
 			Integer result = (Integer) openDao.queryForObject(map);
 			if(result<=0) {
	            map.put("sqlMapId", "api_userrole.update");
	            openDao.update(map);
 			}else {
 				map.put("fk_role", map.get("fk_role_old"));
 				map.put("sqlMapId", "api_userrole.delete");
	            openDao.delete(map);
 			}
               
			return output("0000");
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 * 删除用户角色
	 *
	 */
	@RequestMapping("oAuth/api/deleteUserRoleData")
	public OutputVo deleteUserRoleData() {
		try {
			Map<String, Object> map = getParameterMap();
			map.put("api_name", getApiName(request.getRequestURI()));
			validateApp(map);
			JSONArray list = JSONArray.parseArray(map.get("list").toString());
			if(list.size() > 0) {
				for(int i = 0;i<list.size();i++) {
                    Map<String, Object> vaule = (Map<String, Object>) list.get(i);
                    map.put("fk_role",vaule.get("fk_role"));
                    map.put("fk_user",vaule.get("fk_user"));
                    map.put("update_by",vaule.get("update_by"));
    				map.put("sqlMapId", "api_userrole.delete");
                    openDao.delete(map);
                }
			}
			return output("0000");
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 * 获取任课计划信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getCourseInfoData")
	public OutputVo getCourseInfoData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_courseinfo.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 * 获取学段信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getStageData")
	public OutputVo getStageData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_stage.select");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	
	/**
	 * 获取map地图接口
	 *
	 */
	@RequestMapping("api/getMapJson")
	public OutputVo getMapJson() {
		try {
			String fileName = "static/assets/json/map.json";
			InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
			JSONObject json = JSONObject.parseObject(IOUtils.toString(is, "utf-8"));
			
			return output("0000", "", json);
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
	
	/**
	 *修改学生联系电话接口
	 */
	@RequestMapping("oAuth/api/updateStudentLxdh")
	public OutputVo updateStudentLxdh() {
		try {

			Map<String, Object> map = getParameterMap();
			
			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);
			
			
			map.remove("page");
			
			map.put("sqlMapId", "student.selectLxdh");
			Integer num = (Integer) openDao.queryForObject(map);
			
			if(num == 0) {
				//修改学生联系电话
				map.put("sqlMapId", "student.updateLxdh");
				openDao.update(map);
			}else {
				return output("4444","手机号重复","");
			}
			
			return output("0000","更新成功","");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999","更新失败",e);
		}
	}
	
	/**
	 * 获取班级课表接口
	 *
	 */
	@RequestMapping("oAuth/api/getTimetableData")
	public OutputVo getTimetableData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			String type = "";
			if(map.get("type") != null) {
				type = map.get("type").toString();
			}
			PageVo page = readPage(request);
			if(type.equals("1")) {
				if (null == page) {
					page = new PageVo();
					page.setNumber(50); //默认为一次请求为50条数据
				} else {
					//一次最多返回500条数据
					if (page.getNumber() > 500) {
						page.setNumber(500);
					} else if (page.getNumber() < 1) { //如数量设置错误则修改为默认值
						page.setNumber(50);
					}
				}

				Object currentPage = map.get("currentPage");
				Object number = map.get("number");
				if (null != currentPage) {
					page.setCurrent(Integer.parseInt(currentPage.toString()));
				}
				if (null != number) {
					page.setNumber(Integer.parseInt(number.toString()));
				}

				map.put("page", page);
			} else if(type.equals("2")) {
				map.remove("page");

				Date today = new Date();
				Calendar c = Calendar.getInstance();
				c.setTime(today);
				int weekday = c.get(Calendar.DAY_OF_WEEK);

				map.put("day", weekday-1+"");
			}

			//1.获取当前学年
			map.put("sqlMapId", "api_prop.academic");
			map.put("academic", ((Map)openDao.queryForObject(map)).get("prop_pk"));
			
			//2.获取当前学期
			map.put("sqlMapId", "api_prop.semester");
			map.put("semester", ((Map)openDao.queryForObject(map)).get("prop_pk"));
			
			//3.根据班级id获取学校的id和单双周
			map.put("sqlMapId", "api_prop.week");
			Map<String, Object> school = (Map)openDao.queryForObject(map);
			map.put("fk_unit", school.get("id"));
			map.put("week", school.get("week"));

			map.put("sqlMapId", "api_prop.timetable");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}

	public static JSONObject getDistanceTime(String firstLoginTime, String nowTime) {
		JSONObject dataMap = new JSONObject();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d1;
		try {
			d1 = df.parse(nowTime);
			Date d2 = df.parse(firstLoginTime);
			long diff = d1.getTime() - d2.getTime();//// 当前的时间减去我初次登陆的时间如果大于等于2小时
			long seconds = diff / (1000); // 共计秒数
			long days = diff / (1000 * 60 * 60 * 24);
			long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
			long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
			dataMap.put("hours", hours);
			dataMap.put("minutes", minutes);
			dataMap.put("seconds", seconds);
			dataMap.put("days", days);
			return dataMap;
		} catch (ParseException e) {
			e.printStackTrace();
			return dataMap;
		}
	}

	/**
	 * 获取主管单位信息列表接口
	 *
	 */
	@RequestMapping("oAuth/api/getUnitData")
	public OutputVo getUnitData() {
		try {
			Map<String, Object> map = getParameterMap();

			map.put("api_name", getApiName(request.getRequestURI()));
			validate(map);

			PageVo page = readPage(request);
			if(null == page){
				page = new PageVo();
				page.setNumber(50); //默认为一次请求为50条数据
			}else{
				//一次最多返回500条数据
				if(page.getNumber() > 500){
					page.setNumber(500);
				}else if(page.getNumber() < 1){ //如数量设置错误则修改为默认值
					page.setNumber(50);
				}
			}

			Object currentPage = map.get("currentPage");
			Object number = map.get("number");
			if(null != currentPage){
				page.setCurrent(Integer.parseInt(currentPage.toString()));
			}
			if(null != number){
				page.setNumber(Integer.parseInt(number.toString()));
			}
			map.put("page", page);

			map.put("sqlMapId", "api_school.selectUnit");
			List<Map<String, Object>> dataList = openDao.queryForList(map);
			return output("0000", dataList, page);
		} catch (ExceptionVo e) {
			return output(e.getCode(), e.getMsg(), "");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999", "system exception", "");
		}
	}
}
